﻿#region Includes

using System;
using System.Globalization;

#endregion

namespace ArtheaEngine.Model
{
    public partial class Money
    {
        public override bool Equals(object obj)
        {
            try
            {
                return Value.Equals( ( (Money) obj ).Value );
            }
            catch
            {
                return false;
            }
        }

        public override int GetHashCode()
        {
            return Value.GetHashCode();
        }
        public static Money operator +(Money a, Money b)
        {
            return new Money(a.Value + b.Value);
        }

        public static Money operator -(Money a, Money b)
        {
            return new Money(a.Value - b.Value);
        }

        public static Money operator ++(Money a)
        {
            return new Money(a.Value + 0.01m);
        }

        public static Money operator --(Money a)
        {
            return new Money(a.Value - 0.01m);
        }

        public static Money operator *(Money a, Money b)
        {
            return new Money(a.Value * b.Value);
        }

        public static Money operator /(Money a, Money b)
        {
            return new Money(a.Value / b.Value);
        }

        public static bool operator ==(Money a, Money b)
        {
            return a.Value == b.Value;
        }

        public static bool operator !=(Money a, Money b)
        {
            return a.Value != b.Value;
        }

        public static bool operator >(Money a, Money b)
        {
            return a.Value > b.Value;
        }

        public static bool operator <(Money a, Money b)
        {
            return a.Value < b.Value;
        }

        public static bool operator >=(Money a, Money b)
        {
            return a.Value >= b.Value;
        }

        public static bool operator <=(Money a, Money b)
        {
            return a.Value <= b.Value;
        }

        public static Money operator -(Money a)
        {
            return new Money(-a.Value);
        }

        public static bool operator !(Money a)
        {
            return a.Value == 0;
        }

        public static implicit operator decimal(Money a)
        {
            return a.Value;
        }

        public static implicit operator Money(decimal a)
        {
            return new Money(a);
        }

        public Money(decimal value)
        {
            Value = value;
        }

        public Money()
        { }

        public string ToString(string format)
        {
            return ToString(format, CultureInfo.CurrentCulture);
        }

        public override string ToString()
        {
            return ToString(null);
        }

        public Money Copper()
        {
            var tmp = Value.ToString(".00");

            try
            {
                return int.Parse(tmp[2].ToString());
            }
            catch
            {
                return 0;
            }
        }

        public Money Silver()
        {
            var tmp = Value.ToString(".00");

            try
            {
                return int.Parse(tmp.Remove(0, 1));
            }
            catch
            {
                return 0;
            }
        }


        public Money Gold()
        {
            return Math.Truncate(Value);
        }

        /// <summary>
        /// Values as.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public Money ValueAs(Currency type)
        {
            switch (type)
            {
                default:
                case Currency.Gold:
                    return Gold();
                case Currency.Silver:
                    return Silver() + Gold() * 100;
                case Currency.Copper:
                    return Copper() + Silver() * 10 + Gold() * 100;
            }
        }

        public string ToString(string format, IFormatProvider formatProvider)
        {
            if (format == null) format = "D";

            switch (format.ToUpper())
            {
                case "G":
                    return Gold().Value.ToString("C");
                case "S":
                    return Silver().Value.ToString("C");
                case "C":
                    return Copper().Value.ToString("C");
                default:
                    return Value.ToString(format);
            }
        }
    }

    public enum Currency
    {
        Copper,
        Silver,
        Gold
    }
}